package app.nbdevices.controller;

import app.nbdevices.api.dto.ResponseCode;
import app.nbdevices.modle.revModle.DataChangedModle;
import app.nbdevices.modle.revModle.DeviceInfoModle;
import app.nbdevices.modle.revModle.InfoChangedModle;
import app.nbdevices.modle.revModle.ServiceModle;
import door.controller.DoorController;
import ele.controller.EleController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import water.controller.WaterController;
import yangan.controller.YanganController;

/**
 * 这是设备主控制器，可以接收电信物联网平台上传的12种类型的消息（目前只接收datachanged类型的消息，其他的有需求再做）
 * 并将消息按设备类型分类下发到二级控制器
 */
@RestController
public class DevController {
    @Autowired
    WaterController waterController;

    @Autowired
    EleController eleController;

    @Autowired
    YanganController yanganController;

    @Autowired
    DoorController doorController;

    @PostMapping("/datachanged")
    public void getMsg(@RequestBody DataChangedModle dataChanged){
        System.out.println(dataChanged);
        ServiceModle service=dataChanged.getService();

        switch (service.getServiceId()){
            case "WaterReportData":
                System.out.println("+++++++++++++++++++水表设备周期上报++++++++++++++++++");

                waterController.waterDataReport(dataChanged);
                break;
            case "ReportData":
                System.out.println("+++++++++++++++++++电表设备周期上报++++++++++++++++++");

                eleController.eleReportData(dataChanged);
                break;
            case "cycleDetection":
                System.out.println("+++++++++++++++++++烟感表设备周期上报++++++++++++++++++");

                yanganController.yCycleReportData(dataChanged);
                break;
            case "incidentReport":
                System.out.println("+++++++++++++++++++烟感设备事件上报++++++++++++++++++");

                yanganController.yEventReportData(dataChanged);
            case "doorReport":
                System.out.println("+++++++++++++++++门禁设备上报++++++++++++++++++++++++");

                doorController.doorReportData(dataChanged);
                break;
            case "doorOper":
                System.out.println("+++++++++++++++++门禁设备命令答复++++++++++++++++++++");

                doorController.doorCmdData(dataChanged);
        }
    }

//    @GetMapping("/doorunlock/{addrid}/{openid}")
//    public void doorUnlock(@PathVariable("addrid")String addrid,@PathVariable("openid") String openid){
//        doorController.doorUnlock(addrid,openid);
//    }

    @PostMapping("/infochanged")
    public void getInfo(@RequestBody InfoChangedModle infoChanged){

        DeviceInfoModle deviceInfo=infoChanged.getDeviceInfo();

        switch(deviceInfo.getDeviceType()){

        }
        System.out.println("sss");
    }

    @GetMapping("/dooroper")
    public void openDoor(@RequestParam String internetId){
            doorController.doorUnlock(internetId);
    }

    @GetMapping("/getWaterData/{id}/{internetDevId}")
    public ResponseCode getWaterData(@PathVariable("id") Integer id,@PathVariable("internetDevId") String internetDevId){
        //return ResponseCode.success();
        return ResponseCode.success(waterController.getWaterData(id,internetDevId));
    }

    @GetMapping("/getEleData/{id}/{internetDevId}")
    public ResponseCode getEleData(@PathVariable("id") Integer id,@PathVariable("internetDevId") String internetDevId){
        //return ResponseCode.success();
        return ResponseCode.success(eleController.getWaterData(id,internetDevId));
    }

    @GetMapping("/getSumEle")
    public ResponseCode getSumEle(){
        return ResponseCode.success(eleController.getSumEle());
    }

    @GetMapping("/getSumWater")
    public ResponseCode getSumWater(){
        return ResponseCode.success(waterController.getSumWater());
    }
    @GetMapping("/openDoor")
    public ResponseCode SwitchControl(@RequestParam String internetId){
        doorController.openDoor(internetId);
        return ResponseCode.success();
    }
}
